package com.cws4cloud.cws.mall.basic.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.mall.basic.api.entity.Product;
import com.cws4cloud.cws.mall.basic.api.service.ProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 产品
 *
 * @author zhulj
 * @date 2021-08-15 22:38:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
@Api(value = "product", tags = "产品管理")
public class ProductController {

    private final  ProductService productService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param product 产品
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_product_get')" )
    public R getProductPage(Page page, Product product) {
        return R.ok(productService.page(page, Wrappers.query(product)));
    }


    /**
     * 通过id查询产品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_product_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(productService.getById(id));
    }

    /**
     * 新增产品
     * @param product 产品
     * @return R
     */
    @ApiOperation(value = "新增产品", notes = "新增产品")
    @SysLog("新增产品")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_product_add')" )
    public R save(@RequestBody Product product) {
        return R.ok(productService.save(product));
    }

    /**
     * 修改产品
     * @param product 产品
     * @return R
     */
    @ApiOperation(value = "修改产品", notes = "修改产品")
    @SysLog("修改产品")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_product_edit')" )
    public R updateById(@RequestBody Product product) {
        return R.ok(productService.updateById(product));
    }

    /**
     * 通过id删除产品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除产品", notes = "通过id删除产品")
    @SysLog("通过id删除产品")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_product_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(productService.removeById(id));
    }

}
